package spikes

import (
	"enjoy_xxz/internal/model/database/mysql"
	"gorm.io/gorm"
)

// 秒杀商品关联活动属性表
type SpikesGoodsSku struct {
	gorm.Model
	SpikesID    int64   `gorm:"type:int(10);comment:秒杀活动Id;index;"` //秒杀活动ID  (关联活动表)
	SkuID       int64   `gorm:"type:int(10);comment:SkuID;index;"`      //参与秒杀Sku  (关联商品sku表 )
	SpikesStock int64   `gorm:"type:int(10);comment:秒杀商品库存;"`
	Price       float64 `gorm:"type:decimal(10,2);comment:原价价格;"` //商品原价
	SpikesPrice float64 `gorm:"type:decimal(10,2);comment:秒杀价格;"` //秒杀价格
}

// 查询秒杀商品详情
func (s *SpikesGoodsSku) SpokesStockDetail(skuId, SpikesId int64) (*SpikesGoodsSku, error) {
	return s, mysql.WithMysql(func(db *gorm.DB) error {
		return db.Model(s).Where("sku_id = ?", skuId).Where("spikes_id = ?", SpikesId).First(s).Error
	})
}

// 扣减秒杀商品库存
func (s *SpikesGoodsSku) UpdateSpokesStock(tx *gorm.DB) error {
	return tx.Model(s).Where("sku_id = ?", s.SkuID).Where("spikes_id = ?", s.SpikesID).Update("spikes_stock", gorm.Expr("spikes_stock - ?", s.SpikesStock)).Error
}

// 扣减秒杀商品库存ByskuID
func (s *SpikesGoodsSku) UpdateSpokesStockBySkuId(skuID []int64, num int64) error {
	return mysql.WithMysql(func(db *gorm.DB) error {
		return db.Model(s).Where("sku_id in(?)", skuID).
			Where("spikes_id in(?)", s.SpikesID).
			Update("spikes_stock", gorm.Expr("spikes_stock - ?", num)).Error
	})
}

// 获取秒杀商品列表方法
func (s *SpikesGoodsSku) GetSpikesGoodsList(page, pageSize int64) (list []*SpikesGoodsSku, err error) {
	return list, mysql.WithMysql(func(db *gorm.DB) error {
		siz := (page - 1) * pageSize
		return db.Where("spikes_id = ?", s.SpikesID).Offset(int(siz)).Limit(int(pageSize)).Find(&list).Error
	})
	////哈哈哈哈哈
}
